
#include <BSystem>
#include "member_BMainWindow.h"
#include "member_BWidget.h"

using namespace BWE;

const char* member_BMainWindow::ButtonName(Button button)
{
	switch (button)
	{
	case Button_Close:
		return "close";
	case Button_Minimize:
		return "min";
	case Button_Maximize:
		return "max";
	case Button_Normalize:
		return "normal";
	default:
		return 0;
	}
	return 0;
}
Graph member_BMainWindow::ButtonGraph(Button button)
{
	switch (button)
	{
	case Button_Close:
		return Graph_Cross;
	case Button_Minimize:
		return Graph_Minimize;
	case Button_Maximize:
		return Graph_Maximize;
	case Button_Normalize:
		return Graph_Normalize;
	default:
		return Graph_None;
	}
	return Graph_None;
}

member_BMainWindow::member_BMainWindow(BMainWindow* mainWindow)
{
	boss = mainWindow;
	resizePart = Part_None;
	titleAlign = Align_LeftCenter;
	titleSpace = 5;
	hoverTitle = false;
	pressed = false;
	dirty = true;
	moved = false;
	buttonAlign = Align_RightCenter;
	buttonLayout = new BHBoxLayout();
}
member_BMainWindow::~member_BMainWindow()
{

}

void member_BMainWindow::freshRects()
{
	BSize buttonSize = buttonLayout->sizeHint();
	if (widget_member(boss)->pixmap || boss->parent() || boss->frameless())
	{
		int top = boss->perch(Part_Top);
		if (top < titleIconSize.height())
			top = titleIconSize.height();

		const BFont* font = boss->font();
		if (font && top < font->size())
			top = font->size();

		if (top < buttonSize.height())
			top = buttonSize.height();

		buttonLayout->setVisible(true);
		boss->setPerch(Part_Top, top);
	}
	else
	{
		buttonLayout->setVisible(false);
		boss->setPerch(Part_Top, 0);
	}

	int w = boss->width();
	int h = boss->height();
	int b = boss->border();

	titleRect = boss->rect(Part_Title);
	titleRect.top() += titleMargin.top();
	titleRect.left() += titleMargin.left();
	titleRect.right() -= titleMargin.right();
	titleRect.bottom() -= titleMargin.bottom();
	borderLeft.set(0, b, b, h - b - b);
	borderTop.set(b, 0, w - b - b, b);
	borderRight.set(w - b, b, b, h - b - b);
	borderBottom.set(b, h - b, w - b - b, b);
	borderLeftTop.set(0, 0, b, b);
	borderLeftBottom.set(0, h - b, b, b);
	borderRightTop.set(w - b, 0, b, b);
	borderRightBottom.set(w - b, h - b, b, b);

	buttonLayout->setRect(titleRect.align(buttonAlign, buttonSize));
	buttonLayout->adjust();

	BSize realIconSize = titleIconSize;
	if (realIconSize.width() < 1)
		realIconSize.width() = titleRect.height();
	if (realIconSize.height() < 1)
		realIconSize.height() = titleRect.height();

	BRect iconArea(titleRect.x(), titleRect.y(), 0, titleRect.height());
	BIcon* titleIcon = boss->titleIcon();
	if (titleIcon)
	{
		iconArea.setWidth(titleRect.height());
		if (iconArea.width() < realIconSize.width())
			iconArea.setWidth(realIconSize.width());
		realIconSize.wrap(iconArea.size());
		iconRect = iconArea.align(Align_Center, realIconSize);
	}

	const BFont* font = boss->font();
	if (font && boss->title().size())
	{
		const BStyle* realStyle = boss->realStyle();
		if (realStyle)
			realStyle->translate(boss->title(), tr_title);
		else
			tr_title.clear();

		BSize textSize;
		if (tr_title.size())
			textSize = font->textSize(tr_title);
		else
			textSize = font->textSize(boss->title());
		BRect textArea = titleRect;
		textArea.left() = iconArea.right() + titleSpace;
		textArea.right() = buttonLayout->rect().left() - titleSpace;
		textRect = textArea.align(titleAlign, textSize);
	}
}

void member_BMainWindow::slotTitleButtonClicked(BObject* object, const BValue& value)
{
	if (object->asset() == Button_Close)
	{
		boss->close();
	}
	if (object->asset() == Button_Minimize)
	{
		if (widget_member(boss)->window)
		{
			widget_member(boss)->window->showMinimized();
		}
		if (widget_member(boss)->pixmap)
		{
			boss->hide();
		}
	}
	if (object->asset() == Button_Maximize)
	{
		if (widget_member(boss)->window)
		{
			if (widget_member(boss)->window->isMaximized())
				widget_member(boss)->window->showNormal();
			else
				widget_member(boss)->window->showMaximized();
		}
		if (widget_member(boss)->pixmap)
		{
			BSize size = widget_member(boss)->pixmap->size();
			if (boss->width() > size.width() * 0.96 && boss->height() > size.height() * 0.96)
			{
				boss->setRect(normalRect);
			}
			else
			{
				normalRect = boss->rect();
				boss->setRect(0, 0, size.width(), size.height());
			}
		}
	}
}
